package co.gov.das.fotogps.movil;

import co.gov.das.fotogps.movil.Fotografia;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.location.LocationException;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.68CDC079-942B-82BE-492A-EA354A89E1F8]
// </editor-fold> 
public class Camara extends Canvas implements CommandListener{

    private Player mPlayer;
    private FotoGPSMIDLet mMidlet;
    private VideoControl mVideoControl;
    private Command CAPTURE_CMD;
    private Command VOLVER_CMD;

    public Camara(FotoGPSMIDLet midlet) throws IOException, MediaException {
        CAPTURE_CMD = new Command("Capturar", Command.SCREEN, 0);
        VOLVER_CMD = new Command("Volver", Command.BACK, 0);


        this.addCommand(VOLVER_CMD);
        this.addCommand(CAPTURE_CMD);
        this.setCommandListener(this);

        int width = getWidth();
        int height = getHeight();

        mPlayer = Manager.createPlayer("capture://video");
        mPlayer.realize();

        mVideoControl = (VideoControl) mPlayer.getControl("VideoControl");

        mMidlet = midlet;

        mVideoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
        try {
            mVideoControl.setDisplayLocation(2, 2);
            mVideoControl.setDisplaySize(width - 4, height - 4);
        } catch (MediaException me) {
            try {
                mVideoControl.setDisplayFullScreen(true);
            } catch (MediaException me2) {
            }
        }
        mVideoControl.setVisible(true);
        mPlayer.start();
    }
    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.4F96B2E6-AAA8-8BD0-74D4-907BD24F2A25]
    // </editor-fold> 

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.54847181-50FF-2468-0439-9E9951818A4A]
    // </editor-fold>
    public Image capturarFoto() {
        try {
            // Get the image.
            byte[] raw = mVideoControl.getSnapshot(null);

            Image image = Image.createImage(raw, 0, raw.length);

            Image thumb = createThumbnail(image);

            mMidlet.getFormFotografia().append(thumb);

            // Shut down the player.
            mPlayer.close();
            mPlayer = null;
            mVideoControl = null;
            return thumb;
        } catch (MediaException me) {
            me.printStackTrace();
            handleException(me);
            return null;
        }
    }

    private void handleException(Exception e) {
        Alert a = new Alert("Exception", e.toString(), null, null);
        a.setTimeout(Alert.FOREVER);
        Display.getDisplay(mMidlet).setCurrent(a, mMidlet.getFormFotografia());
    }

    private Image createThumbnail(Image image) {
        int sourceWidth = image.getWidth();
        int sourceHeight = image.getHeight();

        int thumbWidth = 64;
        int thumbHeight = -1;

        if (thumbHeight == -1) {
            thumbHeight = thumbWidth * sourceHeight / sourceWidth;
        }

        Image thumb = Image.createImage(thumbWidth, thumbHeight);
        Graphics g = thumb.getGraphics();

        for (int y = 0; y < thumbHeight; y++) {
            for (int x = 0; x < thumbWidth; x++) {
                g.setClip(x, y, 1, 1);
                int dx = x * sourceWidth / thumbWidth;
                int dy = y * sourceHeight / thumbHeight;
                g.drawImage(image, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
            }
        }

        Image immutableThumb = Image.createImage(thumb);

        return immutableThumb;
    }

    protected void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();

        // Draw a green border around the VideoControl.
        g.setColor(0x00ff00);
        g.drawRect(0, 0, width - 1, height - 1);
        g.drawRect(1, 1, width - 3, height - 3);
    }

    public void keyPressed(int keyCode) {
        int action = getGameAction(keyCode);
        if (action == FIRE) {
            capturarFoto();
            Display.getDisplay(mMidlet).setCurrent(mMidlet.getFormFotografia());
        }
    }

    public void commandAction(Command c, Displayable d) {
        if(c == CAPTURE_CMD){
            Image image = capturarFoto();
            Fotografia fotografia = new Fotografia();
//            fotografia.setFotografia(image.toString().getBytes());
            fotografia.setFotografia("fotografia".getBytes());

            GPS gps = new GPS(mMidlet);
            gps.obtenerCoordenadas();

            fotografia.setLatitud(gps.getLatitud());
            fotografia.setLongitud(gps.getLongitud());

            FormFotografia formFotografia = mMidlet.getFormFotografia();
            formFotografia.setFotografia(fotografia);

            Display.getDisplay(mMidlet).setCurrent(formFotografia);
        }
    }
}
